package com.dragonlance.net;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

public class PackageIn {
	private IoBuffer buff;
	private Charset charset;
	public int code;
	public int extend1;
	public int extend2;
	public int length;
	public IoSession session;
	
	public PackageIn(IoSession session, IoBuffer buff, Charset charset, int length, int code,int extend1, int extend2) {
		this.session = session;
		this.buff = buff;
		this.charset = charset;
		this.length = length;
		this.code = code;
		this.extend1 = extend1;
		this.extend2 = extend2;
		this.buff.flip();
	}

	public int readInt(){
		return this.buff.getInt();
	}
	
	public short readShort(){
		return this.buff.getShort();
	}
	
	public String readUtf() throws CharacterCodingException{
		return this.buff.getString((int)(this.buff.getShort()), this.charset.newDecoder());
	}
	
	public boolean readBoolean(){
		return this.buff.get() == ((byte) 1);
	}
	
	public float readFloat(){
		return this.buff.getFloat();
	}
	
	public double readDouble(){
		return this.buff.getDouble();
	}
}
